package edu.zut.bailan.cart.web.spring.controller;

import edu.zut.bailan.base.web.spring.controller.GenericController;
import edu.zut.bailan.cart.domain.Cart;
import edu.zut.bailan.cart.service.CartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import com.alibaba.fastjson.JSON;

import javax.annotation.Resource;

@Controller
@RequestMapping("/cart")
public class CartController extends GenericController<Cart, Long, CartManager> {

    CartManager cartManager;

    @Resource
    public void setCartManager(CartManager cartManager) {
        this.cartManager = cartManager;
        this.manager = this.cartManager;
    }

    @ResponseBody
    @RequestMapping("/findAll/{page}/{pageSize}")
    public String findAllCart(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(page,pageSize, sort);
        Page<Cart> list = cartManager.findAll(pageable);
        int number = list.getNumber();
        HashMap<String,Object> map = new HashMap<>();
        map.put("number",number);
        map.put("userList",list);

        //map集合转化为JSON字符串进行数据传输
        String res = JSON.toJSONString(map);
        return res;

    }

}

